<template>
    <div>
        <el-form ref="form" label-width="80px">
            <el-form-item label="SPU名称:">
                {{spu.spuName}}
            </el-form-item>
            <el-form-item label="SKU名称">
                <el-input placeholder="SKU名称" v-model="skuInfo.skuName"></el-input>
            </el-form-item>
            <el-form-item label="价格(元)">
                <el-input placeholder="价格" type="number" v-model="skuInfo.price"></el-input>
            </el-form-item>
            <el-form-item label="重量(千克)">
                <el-input placeholder="重量" type="number" v-model="skuInfo.weight"></el-input>
            </el-form-item>
            <el-form-item label="规格描述">
                <el-input type="textarea" rows="5" placeholder="规格描述" v-model="skuInfo.skuDesc"></el-input>
            </el-form-item>
            <el-form-item label="平台属性">
                <el-form :inline="true" ref="form" label-width="80px">
                    <el-form-item style="margin: 5px;" :label="attr.attrName" v-for="(attr,index) in attrInfoList"
                        :key="attr.id">
                        <el-select placeholder="请选择" v-model="attr.attrIdAndAttrValueId">
                            <el-option :label="attrValue.valueName" :value="`${attrValue.id}:${attr.id}`"
                                v-for="(attrValue,index) in attr.attrValueList" :key="attrValue.id">
                            </el-option>
                        </el-select>
                    </el-form-item>

                </el-form>
            </el-form-item>
            <el-form-item label="销售属性">
                <el-form :inline="true" ref="form" label-width="80px">
                    <el-form-item :label="saleAttr.saleAttrName" v-for="(saleAttr,index) in spuSaleAttrList"
                        :key="saleAttr.id">
                        <el-select placeholder="请选择" v-model="saleAttr.attrIdAndValueId">
                            <el-option :label="saleAttrValue.saleAttrValueName"
                                :value="`${saleAttr.id}:${saleAttrValue.id}`"
                                v-for="(saleAttrValue,index) in saleAttr.spuSaleAttrValueList" :key="saleAttrValue.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </el-form-item>
            <el-form-item label="图片列表">
                <el-table border :data="spuImageList" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="60px">
                    </el-table-column>
                    <el-table-column label="图片">
                        <template slot-scope="{row,$index}">
                            <img :src="row.imgUrl" alt="" style="width: 80px;height: 80px;">
                        </template>
                    </el-table-column>
                    <el-table-column label="名称" prop="imgName"></el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="{row,$index}">
                            <el-button type="primary" v-if="row.isDefault==0" @click="changeDefault(row)">设置默认
                            </el-button>
                            <el-button v-else>默认</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="save">保存</el-button>
                <el-button @click="cancel">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
    export default {
        name: 'SkuForm',
        data() {
            return {
                spuImageList: [],
                spuSaleAttrList: [],
                attrInfoList: [],
                skuInfo: {
                    category3Id: 0,
                    spuId: 0,
                    tmId: 0,
                    price: 0,
                    skuName: "",
                    weight: "",
                    skuDesc: "",
                    // 默认图片
                    skuDefaultImg: "",
                    // 平台属性
                    skuAttrValueList: [
                        // {
                        // attrId: 0,
                        // "attrName": "string",
                        // "id": 0,
                        // "skuId": 0,
                        // valueId: 0,
                        // "valueName": "string"
                        // }
                    ],
                    skuImageList: [
                        //{
                        // "id": 0,
                        // "imgName": "string",
                        // "imgUrl": "string",
                        // "isDefault": "string",
                        // "skuId": 0,
                        // "spuImgId": 0
                        // }
                    ],
                    // 收集销售属性列表
                    skuSaleAttrValueList: [
                        // {
                        // "id": 0,
                        // saleAttrId: 0,
                        // "saleAttrName": "string",
                        // saleAttrValueId: 0,
                        // "saleAttrValueName": "string",
                        // "skuId": 0,
                        // "spuId": 0
                        // }
                    ],
                },
                spu: {},
                imgList: [],
            }
        },
        methods: {
            // 获取SKUform数据
            async getData(category1Id, category2Id, spu) {
                // 手机父组件传递的数据
                this.skuInfo.category3Id = spu.category3Id
                this.skuInfo.spuId = spu.id
                this.skuInfo.tmId = spu.tmId
                this.spu = spu
                //    获取图片数据
                let res = await this.$API.sku.reqSpuImageList(spu.id)
                if (res.code == 200) {
                    let list = res.data
                    list.forEach(item => {
                        item.isDefault = 0
                    });
                    this.spuImageList = list
                }
                //获取销售属性的数据
                let res1 = await this.$API.sku.reqSpuSaleAttrList(spu.id)
                if (res1.code == 200) {
                    this.spuSaleAttrList = res1.data
                }
                //获取平台属性的数据
                let res2 = await this.$API.sku.reqAttrInfoList(category1Id, category2Id, spu.category3Id)
                if (res2.code == 200) {
                    this.attrInfoList = res2.data
                }
            },
            //table表格复选框按钮的事件
            handleSelectionChange(params) {
                this.imgList = params
            },
            //排他操作
            changeDefault(row) {
                this.spuImageList.forEach(item => {
                    item.isDefault = 0
                })
                row.isDefault = 1
                this.skuInfo.skuDefaultImg = row.imgUrl
            },
            cancel() {
                this.$emit('changeScenes', 0)
                //清除数据
                Object.assign(this._data, this.$options.data())
            },
            // 保存按钮
            async save() {
                // 整理参数
                //平台属性
                const { attrInfoList, skuInfo, spuSaleAttrList, imgList } = this
                skuInfo.skuAttrValueList = attrInfoList.reduce((prev, item) => {
                    if (item.attrIdAndAttrValueId) {
                        const [attrId, valueId] = item.attrIdAndAttrValueId.split(":");
                        prev.push({ attrId, valueId });
                    }
                    return prev;
                }, []);
                //销售属性
                skuInfo.skuSaleAttrValueList = spuSaleAttrList.reduce((prev, item) => {
                    if (item.attrIdAndValueId) {
                        const [saleAttrId, saleAttrValueId] = item.attrIdAndValueId.split(':');
                        prev.push({ saleAttrId, saleAttrValueId });
                    }
                    return prev;
                }, []);
                //整理图片
                skuInfo.skuImageList = imgList.map(item => {
                    return {
                        imgName: item.imgName,
                        imgUrl: item.imgUrl,
                        isDefault: item.isDefault,
                        spuImgId: item.id,
                    }
                });
                let result = await this.$API.sku.reqAddSku(skuInfo);
                if (result.code == 200) {
                    this.$message({
                        type: 'success',
                        message: '添加成功拉，已创建才会失败'
                    })
                    this.$emit('changeScenes', 0)

                }

            }

        },
    }
</script>
<style lang="">

</style>